(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.siteDate.updateController', updateController);
	updateController.$inject = ['$q', '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'storageService', 'siteService' ];
	function updateController($q, $scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, storageService, siteService) {
		var ctrl = this, siteInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.authService = authService;
		ctrl.site = {};
		
		ctrl.init = function() {
			var siteCode = storageService.getUserInfo().siteCode;
			var params = {
					siteCode : siteCode
				};
			
			ctrl.calendarCodes = ['S:' + siteCode];
			authService.update(params).then(function(response) {
				ctrl.site = response.data || siteInfo;
				ctrl.site.dataStatus = ctrl.site.dataStatus;
				if ("V" != ctrl.site.dataStatus){
					modalService.openErrorDialogs({
						'messages' : ["not verified status, can not update"],
						'btnCallback' : {
							'close' : function() {
								$scope.go('cda.site.list');
							}
						}
					});
				}
			});
		};
		
		ctrl.updateSave = function() {
			if (ctrl.site.taskId) {
				return authService.updateContinueSave(ctrl.site).then(saveSuccessful, saveFailed);
			} else {
				return authService.updateSave(ctrl.site).then(saveSuccessful, saveFailed);
			}
		};
		
		ctrl.updateSubmit = function() {
			if (ctrl.site.taskId) {
				return authService.updateContinueSubmit(ctrl.site).then(submitSuccessful, submitFailed);
			} else {
				return authService.updateSubmit(ctrl.site).then(submitSuccessful, submitFailed);
			}
		};
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedSubCommpanyList = selectedRows;
		}

		function submitSuccessful(response) {		
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.site.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.site.list',
					'state' : 'cda.site.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				ctrl.site = response.data.model;
			}
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
